package com.film.service;

import com.film.entity.Movie;
import com.film.entity.MovieSession;
import com.film.entity.Reservation;
import com.film.entity.Reservations;

import java.util.List;
import java.util.Scanner;

/**
 * @author HustleLr
 * @version 1.0.0
 * @date 2023/12/13 14:09
 * @describe MovieSystemDisplay
 */
public class MovieSystemDisplay {
    private MovieService movieService = new MovieService();

    private ReservationService reservationService = new ReservationService();
    private MovieSessionService movieSessionService = new MovieSessionService();

    public void displayMenu() {
        System.out.println("*************************   Welcome to the movie ticket booking system!    *************************");
        System.out.println("*************************   View movie details Enter: 1                    *************************");
        System.out.println("*************************   View booking details Enter: 2                  *************************");
        System.out.println("*************************   View movie session information Enter: 3        *************************");
        System.out.println("*************************   Movie reservation information enter: 4         *************************");
        System.out.println("*************************   To exit this system, enter: 0                  *************************");
    }

    // 展示电影列表
    public void displayMovies() {
        List<Movie> movies = movieService.getMovies();
        // 假设标题栏的宽度分别为30，10，30字符
        String headerFormat = "%-30s %-20s %-20s%n";
        System.out.format(headerFormat, "movie_name", "movie_duration", "movie_director");

        String rowFormat = "%-30s %-20s %-20s%n";
        for (Movie movie : movies) {
            System.out.format(rowFormat, movie.getTitle(), movie.getDuration() + " minutes", movie.getDirector());
        }
    }

    public void clearScreen() {
        // 打印一定数量的空行
        for (int i = 0; i < 50; ++i) {
            System.out.println();
        }
    }

    /**
     * 展示预定信息
     */
    public void displayReservations() {
        // 展示预订信息的实现代码
        List<Reservation> reservations = reservationService.getReservations();
        System.out.println("Your booking information is below：");
        String headerFormat = "%-10s %-10s %-10s %-30s %-20s %-20s %-20s%n";
        System.out.format(headerFormat, "id", "user_name", "seat_num", "movie_name", "cinema_name", "location", "opening_time");
        String rowFormat = "%-12s %-12s %-12s %-30s %-25s %-20s %-20s%n";
        for (Reservation reservation : reservations) {
            System.out.format(rowFormat, reservation.getReservationId(), reservation.getUser().getName(), reservation.getSeatNumber(),
                    reservation.getMovieSession().getMovie().getTitle(), reservation.getMovieSession().getCinemaHall().getName(),
                    reservation.getMovieSession().getCinemaHall().getLocation(), reservation.getMovieSession().getSessionTime());
        }
    }

    /**
     * 展示电影场次信息
     */
    public void displayMovieHallInfo() {
        List<MovieSession> movieHall = movieSessionService.getMovieHall();
        String headerFormat = "%-10s %-40s %-30s %-30s %-20s%n";
        System.out.format(headerFormat, "id", "movie_name", "cinema_name", "location", "opening_time");
        String rowFormat = "%-10s %-40s %-30s %-30s %-20s%n";
        for (MovieSession movieSession : movieHall) {
            System.out.format(rowFormat, movieSession.getSessionId(), movieSession.getMovie().getTitle() + " minutes",
                    movieSession.getCinemaHall().getName(), movieSession.getCinemaHall().getLocation(), movieSession.getSessionTime());
        }
    }

    /**
     * 预定电影票
     */
    public void displayReservationInfo() {
        displayMovieHallInfo();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the number of the session you want to book：");
        // 场次编号
        String sessionId = scanner.nextLine();
        System.out.println("Please enter the seat number you want to reserve：");
        // 场次编号
        String seatNum = scanner.nextLine();
        // 预定信息
        Reservations reservation = new Reservations();
        reservation.setSeatNumber(seatNum);
        reservation.setUserId("1");
        reservation.setSessionId(sessionId);
        reservationService.saveReservations(reservation);
        displayReservations();
    }
}
